﻿[assembly: System.Web.UI.TagPrefix("GSC.WebControls", "GSC")]
namespace GSC.WebControls
{
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.Design.WebControls;
    using System.Drawing.Design;
    using System.Drawing;
    using System.Web.UI.Design;
    using System.ComponentModel;
    using ComponentModel.Design;
    /// <summary>
    /// 管理后台中间右边控件。
    /// </summary>
    /// <remarks>管理后台中间右边控件。</remarks>
    [ToolboxItemFilter("GSC.WebControls", ToolboxItemFilterType.Custom)]
    [ToolboxData("<{0}:BackgroundCenterRightControl runat=server Height=80px width=800px></{0}:BackgroundCenterRightControl>")]
    [SRDescription("Manage_CenterRight", typeof(Properties.Resources))]
    [Designer(typeof(BackgroundCenterRightControlDesigner))]
    public class BackgroundCenterRightControl : CompositeControl, INamingContainer, ICompositeControlDesignerAccessor, IUpdateAscx
    {
        /// <summary>
        /// 获取或设置背景图片。
        /// </summary>
        /// <remarks>获取或设置背景图片。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_CenterRight_BackImage", typeof(Properties.Resources))]
        [Category("Appearance")]
        public string BackImage
        {
            get
            {
                object o = this.ViewState["BackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["BackImage"] = value;
            }
        }

        /// <summary>
        /// </summary>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        /// <summary>
        /// </summary>
        protected override string TagName
        {
            get
            {
                return "div";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            if (!this.DesignMode)
            {
                base.OnPreRender(e);
            }
            this.Style["overflow"] = "hidden";
            this.Style["background-position"] = "right top";
            this.Style["background-repeat"] = "repeat-y";
            this.Style["padding-bottom"] = this.PaddingBottom.ToString();
            this.Style["padding-left"] = this.PaddingLeft.ToString();
            this.Style["padding-top"] = this.PaddingTop.ToString();
            this.Style["padding-right"] = this.PaddingRight.ToString();
            if (string.IsNullOrEmpty(BackImage))
                this.Style["background-image"] = "url(" + ResolveClientUrl(GetWebResourceUrl("bg.gif")) + ")";
            else
                this.Style["background-image"] = "url(" + ResolveClientUrl(BackImage) + ")";
            //
            this.ControlContainer.Style["text-align"] = "center";
            this.ControlContainer.Style["vertical-align"] = "middle";
            this.ControlContainer.Style["overflow"] = "hidden";
            //if (this.DesignMode) this.ControlContainer.Style["background-color"] = "#FFFFCC";
            this.ControlContainer.Width = Unit.Pixel((int)(this.Width.Value - 4));
            this.ControlContainer.Height = this.Height;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            Page.RegisterRequiresControlState(this);
            base.OnInit(e);
            if (!this.DesignMode)
            {
                this.UpdateAscx();
            }
        }
        /// <summary>
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.DesignMode)
            {
                this.OnPreRender(EventArgs.Empty);
            }
            if (Page != null)
                Page.VerifyRenderingInServerForm(this);
            base.Render(writer);
        }
        PlaceHolder _loadContainer;
        Panel _controlContainer;
        /// <summary>
        /// </summary>
        protected virtual void CreateControlHierarchy()
        {
            this._controlContainer = new Panel();
            this._controlContainer.ID = "ControlContainer";
            if (_loadContainer == null)
                this._loadContainer = new PlaceHolder();
            this._loadContainer.ID = "LoadContainer";
            if (this.DesignMode)
                this._controlContainer.Controls.Add(this._loadContainer);
            else
            {
                this._updatePanel = new UpdatePanel();
                this._updatePanel.ID = "UpdatePanel";
                this._updatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
                this._updatePanel.ContentTemplateContainer.Controls.Add(this._loadContainer);
                this._controlContainer.Controls.Add(this._updatePanel);
            }
            //
            this.Controls.Add(this._controlContainer);
        }
        /// <summary>
        /// 设计时获取资源文件地址。
        /// </summary>
        /// <remarks>设计时获取资源文件地址。</remarks>
        /// <returns>string</returns>
        public virtual string GetWebResourceUrl(string resourceUrl)
        {
            if (this.Page == null) return string.Empty;
            if (this.Page.ClientScript == null) return string.Empty;
            return this.Page.ClientScript.GetWebResourceUrl(typeof(BackgroundCenterRightControl), "GSC.WebControls.BackgroundCenterRightControl.Images." + resourceUrl);
        }

        /// <summary>
        /// </summary>
        protected override void CreateChildControls()
        {
            Controls.Clear();
            CreateControlHierarchy();
            ClearChildViewState();
        }

        void ICompositeControlDesignerAccessor.RecreateChildControls()
        {
            base.ChildControlsCreated = false;
            EnsureChildControls();
        }
        /// <summary>
        /// 加载默认控件。
        /// </summary>
        /// <remarks>加载默认控件。</remarks>
        public void DefaultUpdateAscx()
        {
            UpdateAscx(this.DefaultAscx);
        }
        /// <summary>
        /// 加载控件，如果缓存控件存在侧自动加载缓存控件对象。
        /// </summary>
        /// <remarks>加载控件，如果缓存控件存在侧自动加载缓存控件对象。</remarks>
        public void UpdateAscx()
        {
            UpdateAscx(string.Empty);
        }
        /// <summary>
        /// 加载指定控件。
        /// </summary>
        /// <remarks>加载指定控件。</remarks>
        /// <param name="ascx">设置要加载的控件对象。</param>
        public void UpdateAscx(string ascx)
        {
            this._currentAscx = null;
            this.LoadContainer.Controls.Clear();
            string cacheName_ = this.CacheName;
            switch (this.CacheMode)
            {
                case CacheConversationMode.Application:
                    if (this.Page.Application[cacheName_] == null)
                    {
                        if (string.IsNullOrEmpty(ascx))
                        {
                            if (this.DefaultAscx != string.Empty)
                            {
                                this._currentAscx = this.Page.LoadControl(this.DefaultAscx);
                                this._currentAscx.ID = "Ascx";
                                this._loadContainer.Controls.Add(this._currentAscx);
                                this.Page.Application[cacheName_] = this.DefaultAscx;
                            }
                        }
                        else
                        {
                            this._currentAscx = this.Page.LoadControl(ascx);
                            this._currentAscx.ID = "Ascx";
                            this._loadContainer.Controls.Add(this._currentAscx);
                            this.Page.Application[cacheName_] = ascx;
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(ascx))
                        {
                            this._currentAscx = this.Page.LoadControl(this.Page.Application[cacheName_].ToString());
                            this._currentAscx.ID = "Ascx";
                            this._loadContainer.Controls.Add(this._currentAscx);
                        }
                        else
                        {
                            this.Page.Application[cacheName_] = ascx;
                            this._currentAscx = this.Page.LoadControl(ascx);
                            this._currentAscx.ID = "Ascx";
                            this._loadContainer.Controls.Add(this._currentAscx);
                        }
                    }
                    break;
                case CacheConversationMode.Cache:
                    if (this.Page.Cache[cacheName_] == null)
                    {
                        if (string.IsNullOrEmpty(ascx))
                        {
                            if (this.DefaultAscx != string.Empty)
                            {
                                this._currentAscx = this.Page.LoadControl(this.DefaultAscx);
                                this._currentAscx.ID = "Ascx";
                                this._loadContainer.Controls.Add(this._currentAscx);
                                this.Page.Cache[cacheName_] = this.DefaultAscx;
                            }
                        }
                        else
                        {
                            this._currentAscx = this.Page.LoadControl(ascx);
                            this._currentAscx.ID = "Ascx";
                            this._loadContainer.Controls.Add(this._currentAscx);
                            this.Page.Cache[cacheName_] = ascx;
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(ascx))
                        {
                            this._currentAscx = this.Page.LoadControl(this.Page.Cache[cacheName_].ToString());
                            this._currentAscx.ID = "Ascx";
                            this._loadContainer.Controls.Add(this._currentAscx);
                        }
                        else
                        {
                            this.Page.Cache[cacheName_] = ascx;
                            this._currentAscx = this.Page.LoadControl(ascx);
                            this._currentAscx.ID = "Ascx";
                            this._loadContainer.Controls.Add(this._currentAscx);
                        }
                    }
                    break;
                case CacheConversationMode.Cookie:
                    HttpCookie ascxCookie = this.Page.Request.Cookies[cacheName_];
                    if (ascxCookie == null)
                    {
                        if (string.IsNullOrEmpty(ascx))
                        {
                            if (this.DefaultAscx != string.Empty)
                            {
                                this._currentAscx = this.Page.LoadControl(this.DefaultAscx);
                                this._currentAscx.ID = "Ascx";
                                this._loadContainer.Controls.Add(this._currentAscx);
                                ascxCookie = new HttpCookie(cacheName_);
                                ascxCookie.Expires = DateTime.MaxValue;
                                ascxCookie.Value = this.DefaultAscx;
                                this.Page.Response.Cookies.Add(ascxCookie);
                            }
                        }
                        else
                        {
                            this._currentAscx = this.Page.LoadControl(ascx);
                            this._currentAscx.ID = "Ascx";
                            this._loadContainer.Controls.Add(this._currentAscx);
                            ascxCookie = new HttpCookie(cacheName_);
                            ascxCookie.Expires = DateTime.MaxValue;
                            ascxCookie.Value = ascx;
                            this.Page.Response.Cookies.Add(ascxCookie);
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(ascx))
                        {
                            this._currentAscx = this.Page.LoadControl(ascxCookie.Value);
                            this._currentAscx.ID = "Ascx";
                            this._loadContainer.Controls.Add(this._currentAscx);
                        }
                        else
                        {
                            ascxCookie.Value = ascx;
                            this._currentAscx = this.Page.LoadControl(ascx);
                            this._currentAscx.ID = "Ascx";
                            this._loadContainer.Controls.Add(this._currentAscx);
                        }
                        this.Page.Response.Cookies.Set(ascxCookie);
                    }
                    break;
                case CacheConversationMode.Session:
                    if (this.Page.Session[cacheName_] == null)
                    {
                        if (string.IsNullOrEmpty(ascx))
                        {
                            if (this.DefaultAscx != string.Empty)
                            {
                                this._currentAscx = this.Page.LoadControl(this.DefaultAscx);
                                this._currentAscx.ID = "Ascx";
                                this._loadContainer.Controls.Add(this._currentAscx);
                                this.Page.Session[cacheName_] = this.DefaultAscx;
                            }
                        }
                        else
                        {
                            this._currentAscx = this.Page.LoadControl(ascx);
                            this._currentAscx.ID = "Ascx";
                            this._loadContainer.Controls.Add(this._currentAscx);
                            this.Page.Session[cacheName_] = ascx;
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(ascx))
                        {
                            this._currentAscx = this.Page.LoadControl(this.Page.Session[cacheName_].ToString());
                            this._currentAscx.ID = "Ascx";
                            this._loadContainer.Controls.Add(this._currentAscx);
                        }
                        else
                        {
                            this.Page.Session[cacheName_] = ascx;
                            this._currentAscx = this.Page.LoadControl(ascx);
                            this._currentAscx.ID = "Ascx";
                            this._loadContainer.Controls.Add(this._currentAscx);
                        }
                    }
                    break;
            }
            if (this._updatePanel != null)
                this._updatePanel.Update();
        }
        /// <summary>
        /// 加载指定控件，要加载的控件必须实现 GuoShuCan.SDK.Web.IAscxFileName 接口。
        /// </summary>
        /// <remarks>加载指定控件，要加载的控件必须实现 GuoShuCan.SDK.Web.IAscxFileName 接口。</remarks>
        /// <param name="ascx">设置要加载的控件对象。</param>
        public void UpdateAscx(Control ascx)
        {
            if (ascx == null || !(ascx is UserControl)) throw new ArgumentOutOfRangeException();
            this.LoadContainer.Controls.Clear();
            string cacheName_ = this.CacheName;
            switch (this.CacheMode)
            {
                case CacheConversationMode.Application:
                    this.Page.Application[cacheName_] = ascx.TemplateSourceDirectory + "/" + ((IAscxFileName)ascx).AscxFileName;
                    ascx.ID = "Ascx";
                    this.LoadContainer.Controls.Add(ascx);
                    break;
                case CacheConversationMode.Cache:
                    this.Page.Cache[cacheName_] = ascx.TemplateSourceDirectory + "/" + ((IAscxFileName)ascx).AscxFileName;
                    ascx.ID = "Ascx";
                    this.LoadContainer.Controls.Add(ascx);
                    break;
                case CacheConversationMode.Cookie:
                    HttpCookie ascxCookie = this.Page.Request.Cookies[cacheName_];
                    if (ascxCookie == null)
                    {
                        ascxCookie = new HttpCookie(cacheName_);
                        ascxCookie.Expires = DateTime.MaxValue;
                        ascxCookie.Value = ascx.TemplateSourceDirectory + "/" + ((IAscxFileName)ascx).AscxFileName;
                        this.Page.Response.Cookies.Add(ascxCookie);
                    }
                    else
                    {
                        ascxCookie.Value = ascx.TemplateSourceDirectory + "/" + ((IAscxFileName)ascx).AscxFileName;
                        this.Page.Response.Cookies.Set(ascxCookie);
                    }
                    ascx.ID = "Ascx";
                    this.LoadContainer.Controls.Add(ascx);
                    break;
                case CacheConversationMode.Session:
                    this.Page.Session[cacheName_] = ascx.TemplateSourceDirectory + "/" + ((IAscxFileName)ascx).AscxFileName;
                    ascx.ID = "Ascx";
                    this.LoadContainer.Controls.Add(ascx);
                    break;
            }
            if (this._updatePanel != null)
                this._updatePanel.Update();
        }
        /// <summary>
        /// 获取内容容器。
        /// </summary>
        /// <remarks>获取内容容器。</remarks>
        /// <value>System.Web.UI.WebControls.Panel</value>
        [Browsable(false)] 
        public System.Web.UI.WebControls.Panel ControlContainer
        {
            get
            {
                return _controlContainer;
            }
        }
        /// <summary>
        /// 获取加载容器。
        /// </summary>
        /// <remarks>获取加载容器。</remarks>
        /// <value>System.Web.UI.WebControls.PlaceHolder</value>
        [Browsable(false)]
        public PlaceHolder LoadContainer
        {
            get
            {
                if (_loadContainer == null) _loadContainer = new PlaceHolder();
                return _loadContainer;
            }
        }
        UpdatePanel _updatePanel;
        /// <summary>
        /// 获取内容更行对象。
        /// </summary>
        /// <remarks>获取内容更行对象。</remarks>
        /// <value>System.Web.UI.UpdatePanel</value>
        [Browsable(false)] 
        public UpdatePanel UpdatePanel
        {
            get
            {
                return _updatePanel;
            }
        }

        /// <summary>
        /// 获取或设置加载控件容器下边距离。
        /// </summary>
        /// <remarks>获取或设置加载控件容器下边距离。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [DefaultValue(typeof(Unit), "")]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_CenterRight_PaddingBottom", typeof(Properties.Resources))]
        [Category("Layout")]
        [TypeConverter(typeof(UnitConverter))]
        public Unit PaddingBottom
        {
            get
            {
                object o = this.ViewState["PaddingBottom"];
                if (o == null) return Unit.Empty;
                return (Unit)o;
            }
            set
            {
                this.ViewState["PaddingBottom"] = value;
            }
        }

        /// <summary>
        /// 获取或设置加载控件容器左边距离。
        /// </summary>
        /// <remarks>获取或设置加载控件容器左边距离。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [DefaultValue(typeof(Unit), "8px")]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_CenterRight_PaddingLeft", typeof(Properties.Resources))]
        [Category("Layout")]
        [TypeConverter(typeof(UnitConverter))]
        public Unit PaddingLeft
        {
            get
            {
                object o = this.ViewState["PaddingLeft"];
                if (o == null) return Unit.Pixel(8);
                return (Unit)o;
            }
            set
            {
                this.ViewState["PaddingLeft"] = value;
            }
        }

        /// <summary>
        /// 获取或设置加载控件容器右边距离。
        /// </summary>
        /// <remarks>获取或设置加载控件容器右边距离。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [DefaultValue(typeof(Unit), "8px")]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_CenterRight_PaddingRight", typeof(Properties.Resources))]
        [Category("Layout")]
        [TypeConverter(typeof(UnitConverter))]
        public Unit PaddingRight
        {
            get
            {
                object o = this.ViewState["PaddingRight"];
                if (o == null) return Unit.Pixel(8);
                return (Unit)o;
            }
            set
            {
                this.ViewState["PaddingRight"] = value;
            }
        }

        /// <summary>
        /// 获取或设置加载控件容器上边距离。
        /// </summary>
        /// <remarks>获取或设置加载控件容器上边距离。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [DefaultValue(typeof(Unit), "")]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_CenterRight_PaddingTop", typeof(Properties.Resources))]
        [Category("Layout")]
        [TypeConverter(typeof(UnitConverter))]
        public Unit PaddingTop
        {
            get
            {
                object o = this.ViewState["ItemPaddingTop"];
                if (o == null) return Unit.Empty;
                return (Unit)o;
            }
            set
            {
                this.ViewState["ItemPaddingTop"] = value;
            }
        }
        /// <summary>
        /// 获取或设置控件容器加载的控件缓存模式。
        /// </summary>
        /// <remarks>获取或设置控件容器加载的控件缓存模式。</remarks>
        /// <value>GuoShuCan.SDK.Web.CacheConversationMode</value>
        [DefaultValue(typeof(CacheConversationMode), "Cookie")]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_CenterRight_CacheMode", typeof(Properties.Resources))]
        [Category("Behavior")]
        public CacheConversationMode CacheMode
        {
            get
            {
                object o = this.ViewState["CacheMode"];
                if (o == null) return CacheConversationMode.Cookie;
                return (CacheConversationMode)o;
            }
            set
            {
                this.ViewState["CacheMode"] = value;
            }
        }
        /// <summary>
        /// 获取设置默认加载控件。
        /// </summary>
        /// <remarks>获取设置默认加载控件。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Editor(typeof(UrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_CenterRight_DefaultAscx", typeof(Properties.Resources))]
        [Category("Behavior")]
        public string DefaultAscx
        {
            get
            {
                object o = this.ViewState["DefaultAscx"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["DefaultAscx"] = value;
            }
        }
        /// <summary>
        /// 获取或设置控件容器加载的控件缓存名称。
        /// </summary>
        /// <remarks>获取或设置控件容器加载的控件缓存名称。</remarks>
        /// <value>string</value>
        [DefaultValue("Ascx")]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_CenterRight_CacheName", typeof(Properties.Resources))]
        [Category("Behavior")]
        public string CacheName
        {
            get
            {
                object o = this.ViewState["CacheName"];
                if (o == null) return "Ascx";
                return (string)o;
            }
            set
            {
                this.ViewState["CacheName"] = value;
            }
        }

        /// <summary>
        /// 获取当前加载的用户控件对象。
        /// </summary>
        /// <remarks>获取当前加载的用户控件对象。</remarks>
        /// <value>System.Web.UI.Control</value>
        [Browsable(false)]
        public Control CurrentAscx
        {
            get
            {
                return _currentAscx;
            }
        }

        Control _currentAscx;
    }
}
